import { sequelize, Group, UserGroup, User, Task, Document, Progress, Answer, SttsDaily } from '../models'
import _ from 'lodash'
import fdb from '../lib/fdb'
import dayjs from 'dayjs'

export const daily = async (date = dayjs().format('YYYY-MM-DD')) => {
  let st = date + ' 00:00:00'
  let et = date + ' 23:59:59'
  let newUserNum = 0
  let users = await User.findAll({
    attributes: ['id'],
    where: {
      $and: [{createdAt: {$gt: st}}, {createdAt: {$lte: et}}]
    }
  })
  let uids = users.map(n => n.id)
  newUserNum = users.length

  let newTeacherNum = 0
  let docs = await Document.findAll({
    attributes: ['id', 'userId'],
    where: {
      userId: uids
    },
    group: ['userId']
  })
  newTeacherNum = docs.length

  let publishHomeworkTeacherNum = 0
  let tasks = await Task.findAll({
    attributes: ['id', 'ownerId'],
    where: {
      $and: [{createdAt: {$gt: st}}, {createdAt: {$lte: et}}]
    },
    group: ['ownerId']
  })
  publishHomeworkTeacherNum = tasks.length

  let publishHomeworkGroupNum = 0
  tasks = await Task.findAll({
    attributes: ['id', 'ownerId'],
    where: {
      $and: [{createdAt: {$gt: st}}, {createdAt: {$lte: et}}]
    },
    group: ['groupId']
  })
  publishHomeworkGroupNum = tasks.length

  let receivedHomeworkStudentNum = 0
  let progresses = await Progress.findAll({
    attributes: ['id', 'state'],
    where: {
      $and: [{createdAt: {$gt: st}}, {createdAt: {$lte: et}}]
    },
  })
  receivedHomeworkStudentNum = progresses.length

  let doneHomeworkStudentNum = 0
  let doneProgresses = progresses.filter(n => n.state != 'start')
  doneHomeworkStudentNum = doneProgresses.length

  let checkHomeworkStudentNum = 0
  let checkProgresses = progresses.filter(n => n.state == 'check')
  checkHomeworkStudentNum = checkProgresses.length

  let redoHomeworkStudentNum = 0
  let pids = progresses.map(n => n.id)
  let ans = await Answer.findAll({
    attributes: [['count(*)', 'uc'], 'id'],
    where: {
      progressId: pids
    },
    group: ['userId'],
    raw: true,
    having: {
      uc: {$gt: 1}
    }
  })
  redoHomeworkStudentNum = ans.length

  let createdDate = date
  let stts = await SttsDaily.findOne({
    where: {
      createdDate
    }
  })
  if (stts) {
    stts.newTeacherNum = newTeacherNum
    stts.newUserNum = newUserNum
    stts.publishHomeworkTeacherNum = publishHomeworkTeacherNum
    stts.publishHomeworkGroupNum = publishHomeworkGroupNum
    stts.receivedHomeworkStudentNum = receivedHomeworkStudentNum
    stts.doneHomeworkStudentNum = doneHomeworkStudentNum
    stts.checkHomeworkStudentNum = checkHomeworkStudentNum
    stts.redoHomeworkStudentNum = redoHomeworkStudentNum
    stts.createdDate = createdDate
    await stts.save()
  } else {
    await SttsDaily.create({
      newTeacherNum,
      newUserNum,
      publishHomeworkTeacherNum,
      publishHomeworkGroupNum,
      receivedHomeworkStudentNum,
      doneHomeworkStudentNum,
      checkHomeworkStudentNum,
      redoHomeworkStudentNum,
      createdDate
    })
  }
}
